package net.arogarth.aws.android.manager.views;

import java.util.ArrayList;
import java.util.List;

import net.arogarth.aws.android.lib.AmazonEc2;
import net.arogarth.aws.android.lib.AmazonRoute53;
import net.arogarth.aws.android.lib.AwsRequestException;
import net.arogarth.aws.android.lib.models.Ec2ElasticIp;
import net.arogarth.aws.android.lib.models.Ec2ElasticIp.Ec2Domain;
import net.arogarth.aws.android.lib.models.Ec2Instance;
import net.arogarth.aws.android.lib.models.HostetZone;
import net.arogarth.aws.android.lib.models.HostetZoneRecordSet;
import net.arogarth.aws.android.manager.R;
import net.arogarth.aws.android.manager.views.ElIpList.ElIpListAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Route53HostetZoneRecordSetList extends Activity {
	public class Route53HostetZoneRecordSetListAdapter extends BaseAdapter implements OnItemClickListener {

		private final List<HostetZoneRecordSet> r53HostetZones = new ArrayList<HostetZoneRecordSet>();
		public String hZoneId = "";
		
		private Context context;

		public Route53HostetZoneRecordSetListAdapter(Context view, ListView list, String hZone) {
			this.context = view;
			this.hZoneId = hZone;
			
			list.setAdapter(this);
			list.setOnItemClickListener(this);
			
			this.loadData();
		}

		private void loadData() {
			final ProgressDialog pd = ProgressDialog.show(this.context,
					"Please Wait...", "Loading Data...", true, false);

			final String _hZoneId = this.hZoneId;
			
			final Thread t = new Thread() {
				public void run() {
					AmazonRoute53 ar53 = new AmazonRoute53();

					try {
						List<HostetZoneRecordSet> _zones= ar53.listHostetZoneRecordSets(_hZoneId);

						r53HostetZones.clear();
						for (HostetZoneRecordSet zone : _zones) {
							r53HostetZones.add(zone);
						}
						
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								notifyDataSetChanged();
								pd.dismiss();
							}
						});
					} catch (final AwsRequestException e) {
//						runOnUiThread(new Runnable() {
//							public void run() {
//								Toast.makeText(_context, e.getMessage(), Toast.LENGTH_SHORT).show();
//								finish();
//							}
//						});
					}
				}
			};
			t.start();
		}

		public int getCount() {
			return this.r53HostetZones.size();
		}

		public HostetZoneRecordSet getItem(int arg0) {
			return (HostetZoneRecordSet) this.r53HostetZones.get(arg0);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			final HostetZoneRecordSet zone = (HostetZoneRecordSet) getItem(position);

			final LinearLayout _ll = (LinearLayout) LayoutInflater.from(
					this.context).inflate(R.layout.route53_hostedzone_recordset_list_item, parent,
					false);

			((TextView) _ll.findViewById(R.id.zoneName)).setText(zone.getName());
			((TextView) _ll.findViewById(R.id.tvRecordType)).setText(zone.getType().toString());
			
			return _ll;
		}

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			HostetZoneRecordSet recordSet = this.getItem(arg2);
			
			Bundle b = new Bundle();
			b.putString("rsName", recordSet.getName());
			b.putString("rsType", recordSet.getType().toString());
			b.putString("rsDnsName", recordSet.getDnsName());
			b.putString("rsTtl", recordSet.getTtl());
			String rsRecords = "";
//			for(String r : recordSet.getResourceRecords())
//				rsRecords += (rsRecords==""?"":"\n") + r;
			b.putString("rsResourceRecord", rsRecords);
			b.putString("rsWeight", recordSet.getWeight());
			b.putString("rsSetIdentifier", recordSet.getSetIdentifier());
			b.putString("rsHostedZoneId", recordSet.getHostedZoneId());

			Intent i = new Intent(this.context, Route53HostetZoneRecordSetDetails.class);
			i.putExtras(b);
			
			this.context.startActivity(i);
		}
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.basic_list);
		final ListView lv = (ListView) findViewById(R.id.contentlist);
		String hZone = getIntent().getExtras().getCharSequence("hZoneId").toString();
		
		new Route53HostetZoneRecordSetListAdapter(Route53HostetZoneRecordSetList.this, lv, hZone);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.route53_hostetzone_recordsets, menu);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		startActivity(new Intent(Route53HostetZoneRecordSetList.this, Route53HostetZoneRecordSetEdit.class));
		return true;
	}
}
